/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.animation;

import com.bytedance.scene.Scene;
import com.bytedance.scene.animation.animatorexecutor.DefaultSceneAnimatorExecutor;
import com.bytedance.scene.utlity.CancellationSignal;
import com.bytedance.scene.utlity.Utility;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.transition.Transition;

/**
 * Created by JiangQi on 8/15/18.
 * <p>
 * TODO: Translucent is not supported now.
 * <p>
 * use SharedElementSceneTransitionExecutor or ActivityToSceneSharedElementSceneTransitionExecutor instead
 */
@Deprecated
public abstract class NavigationTransitionExecutor extends NavigationAnimationExecutor {
    @Override
    public boolean isSupport(Class<? extends Scene> from, Class<? extends Scene> to) {
        return true;
    }

    protected abstract Transition getSharedElementTransition();

    protected abstract Transition getOthersTransition();

    private NavigationAnimationExecutor mFallbackAnimationExecutor;

    public NavigationTransitionExecutor(NavigationAnimationExecutor fallbackAnimationExecutor) {
        this.mFallbackAnimationExecutor = fallbackAnimationExecutor;
    }

    public NavigationTransitionExecutor() {
        this(new DefaultSceneAnimatorExecutor());
    }

    @Override
    public final void executePushChangeCancelable(AnimationInfo fromInfo, final AnimationInfo toInfo,
                                                  final Runnable endAction, CancellationSignal cancellationSignal) {
        if (fromInfo.mIsTranslucent || toInfo.mIsTranslucent) {
            throw new IllegalArgumentException("SharedElement animation don't support translucent scene");
        }

        executePushChangeV21(fromInfo, toInfo, endAction, cancellationSignal);
    }

    private static void sss(Transition transition, Component v) {
        if (transition == null) {
            return;
        }
        if (v instanceof ComponentContainer) {
//            transition.excludeTarget(v, true);
            for (int i = 0; i < ((ComponentContainer) v).getChildCount(); i++) {
                sss(transition, ((ComponentContainer) v).getComponentAt(i));
            }
        }
    }

    private void executePushChangeV21(AnimationInfo fromInfo, AnimationInfo toInfo,
                                      final Runnable endAction, CancellationSignal cancellationSignal) {
        final Component fromView = fromInfo.mSceneView;
        final Component toView = toInfo.mSceneView;
        final ComponentContainer root = (ComponentContainer) fromView.getComponentParent();

        fromView.setVisibility(Component.VISIBLE);
        toView.setVisibility(Component.VISIBLE);

        Utility.removeFromParentView(fromView);
        Utility.removeFromParentView(toView);

        mAnimationViewGroup.addComponent(fromView);

//        final TransitionSet transitionSet = new TransitionSet();
//        transitionSet.setDuration(2500);

//        Transition share = getSharedElementTransition();
//        transitionSet.addTransition(share);

        Transition other = getOthersTransition();
        sss(other, fromView);
        sss(other, toView);

//        if (other != null) {
//            transitionSet.addTransition(other);
//        }
//        transitionSet.addListener(new TransitionListenerAdapter() {
//            @Override
//            public void onTransitionEnd(Transition transition) {
//                super.onTransitionEnd(transition);
//
//                Utility.removeFromParentView(fromView);
//                Utility.removeFromParentView(toView);
//
//                root.addComponent(fromView);
//                fromView.setVisibility(Component.HIDE);
//                root.addComponent(toView);
//                endAction.run();
//            }
//        });

//        TransitionManager.beginDelayedTransition(mAnimationViewGroup, transitionSet);

        mAnimationViewGroup.removeComponent(fromView);
        mAnimationViewGroup.addComponent(toView);
        cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
//                TransitionManager.endTransitions(mAnimationViewGroup);
            }
        });
    }

    @Override
    public final void executePopChangeCancelable(AnimationInfo fromInfo, AnimationInfo toInfo,
                                                 final Runnable endAction, CancellationSignal cancellationSignal) {
        if (fromInfo.mIsTranslucent || toInfo.mIsTranslucent) {
            throw new IllegalArgumentException("SharedElement animation don't support translucent scene");
        }

        executePopChangeV21(fromInfo, toInfo, endAction, cancellationSignal);
    }

    private void executePopChangeV21(AnimationInfo fromInfo, AnimationInfo toInfo,
                                     final Runnable endAction, CancellationSignal cancellationSignal) {
        final Component fromView = fromInfo.mSceneView;
        final Component toView = toInfo.mSceneView;
        final ComponentContainer root = (ComponentContainer) toView.getComponentParent();

        fromView.setVisibility(Component.VISIBLE);
        toView.setVisibility(Component.VISIBLE);

        Utility.removeFromParentView(fromView);
        Utility.removeFromParentView(toView);

        mAnimationViewGroup.addComponent(fromView);

//        TransitionSet transitionSet = new TransitionSet();
//        transitionSet.setDuration(2500);
//
//        Transition share = getSharedElementTransition();
//        transitionSet.addTransition(share);

        Transition other = getOthersTransition();
        sss(other, fromView);
        sss(other, toView);

//        if (other != null) {
//            transitionSet.addTransition(other);
//        }
//        transitionSet.addListener(new TransitionListenerAdapter() {
//            @Override
//            public void onTransitionEnd(Transition transition) {
//                super.onTransitionEnd(transition);
//
//                Utility.removeFromParentView(fromView);
//                Utility.removeFromParentView(toView);
//
//                root.addComponent(toView);
//                endAction.run();
//            }
//        });

//        TransitionManager.beginDelayedTransition(mAnimationViewGroup, transitionSet);

        mAnimationViewGroup.removeComponent(fromView);
        mAnimationViewGroup.addComponent(toView);

        cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
//                TransitionManager.endTransitions(mAnimationViewGroup);
            }
        });
    }
}
